package com.example.smartcity_poverty.ui.AllService.PetHospital.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.PetHospitalBean.PhAllListModel
import com.example.smartcity_poverty.logic.ServiceBean.PetHospitalBean.PhAllUserListModel
import com.example.smartcity_poverty.logic.ServiceBean.PetHospitalBean.PhMeListAllModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.PetHospital.adapter.PhMeShowUserListAdapter
import kotlinx.android.synthetic.main.activity_ph_me_show.*
import kotlinx.coroutines.Dispatchers
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

/**
 * 问诊详情页面
 */
class PhMeShowActivity : AppCompatActivity() {

    private val rows by lazy { intent.getSerializableExtra("rows") as PhMeListAllModel.RowsBean }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_ph_me_show)
        title = "问诊详情"

        // 填充数据
        glide(url + rows.doctor?.avatar, jsMeShowImage)
        jsMeShowName.text = rows.doctor?.jobName
        jsMeShowTv1.text = "医生职称: ${rows.doctor?.name}"
        jsMeShowTv2.text = "执业编号: ${rows.doctor?.practiceNo}"
        jsMeShowTv3.text = "咨询描述: ${rows.question}"


        setAllUserList()

        // 提交评论
        setOk()
    }

    private val arrayUserList = ArrayList<PhAllUserListModel.RowsBean>()
    private fun setAllUserList() {
        coroutine(Dispatchers.Main) {
            api.getPhAllUserList(
                getToken(),
                mapOf("pageNum" to 1, "pageSize" to 50, "inquiryId" to rows.doctor?.id!!)
            ).apply {
                if (this.code == 200) {
                    arrayUserList.addAll(this.rows!!)
                    runOnUiThread {
                        jsMeShowListRv.apply {
                            this.isNestedScrollingEnabled = false
                            this.layoutManager = LinearLayoutManager(context)
                            this.adapter = PhMeShowUserListAdapter(
                                arrayUserList,
                                R.layout.re_js_me_show_user_list
                            )
                            this.adapter?.notifyItemChanged(arrayUserList.size)
                        }
                    }
                } else runOnUiThread { this.msg?.show() }
            }
        }

    }

    /**
     * 提交评论
     */
    private fun setOk() {
        jsMeShowOk.setOnClickListener {
            if (jsMeShowEditText.text.toString().isNotEmpty()) {
                coroutine(Dispatchers.Main) {
                    api.getAddUser(getToken(), JSONObject().apply {
                        put("inquiryId", rows.doctor?.id)
                        put("content", jsMeShowEditText.text.toString())
                    }.toString().toRequestBody("application/json".toMediaTypeOrNull())).apply {
                        if (this.code == "200") {
                            // 更新数据ui
                            coroutine(Dispatchers.Main) {
                                api.getPhAllUserList(
                                    getToken(),
                                    mapOf(
                                        "pageNum" to 1,
                                        "pageSize" to 50,
                                        "inquiryId" to rows.doctor?.id!!
                                    )
                                ).apply {
                                    if (this.code == 200) {
                                        jsMeShowEditText.setText("")
                                        // 清除原先的数据
                                        arrayUserList.clear()
                                        // 重新添加数据
                                        arrayUserList.addAll(this.rows!!)
                                        runOnUiThread {
                                            jsMeShowListRv.apply {
                                                this.isNestedScrollingEnabled = false
                                                this.layoutManager = LinearLayoutManager(context)
                                                this.adapter = PhMeShowUserListAdapter(
                                                    arrayUserList,
                                                    R.layout.re_js_me_show_user_list
                                                )
                                                this.adapter?.notifyItemChanged(arrayUserList.size)
                                            }
                                        }
                                    } else runOnUiThread { this.msg?.show() }
                                }
                            }
                        } else runOnUiThread { this.msg.show() }

                    }
                }
            }
        }
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}